import Vue from 'vue'
import Vuex from 'vuex'
import {resetRouter} from '../router/index'
Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    user: {
      empId:window.localStorage.getItem('user' || '[]') == null ? '' : JSON.parse(window.localStorage.getItem('user' || '[]')).empId,
      empName: window.localStorage.getItem('user' || '[]') == null ? '未登录' : JSON.parse(window.localStorage.getItem('user' || '[]')).empName,
      empImgpath:window.localStorage.getItem('user' || '[]') == null ? '' : JSON.parse(window.localStorage.getItem('user' || '[]')).empImgpath,
    },
    children:[],
  },
  mutations: {
    initMenu(state, menus){
      //将获取的菜单信息，装载到children对象中
      state.children = menus;
      window.console.log("Menu存储成功");
      window.localStorage.setItem('children',JSON.stringify(menus));
    },
    //登陆
    login (state, user) {
      //添加用户信息到状态管理器中
      state.user = user
      //在localStorage中添加用户，字符串对象
      //JSON.stringify 将对象序列化为JSON格式的字符串
      window.localStorage.setItem('user', JSON.stringify(user))
    },
    //登出
    loginOut(state){
      window.localStorage.clear();
      state.user=[];
      state.children = [];
      resetRouter();
      window.console.log("无情调用");
    }


  }
})
